package com.hrzyit.whistle.cases.ui.activity

import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.hrzyit.base.common.BaseConstant
import com.hrzyit.base.ext.onClick
import com.hrzyit.base.ui.activity.BaseMvpActivity
import com.hrzyit.base.utils.AppPrefsUtils
import com.hrzyit.whistle.cases.R
import com.hrzyit.whistle.cases.data.bean.CaseIndexUserInfoVo
import com.hrzyit.whistle.cases.injection.component.DaggerCaseCenterComponent
import com.hrzyit.whistle.cases.injection.module.CaseCenterModule
import com.hrzyit.whistle.cases.presenter.SelectDeptUserPresenter
import com.hrzyit.whistle.cases.presenter.view.SelectDeptUserView
import com.hrzyit.whistle.cases.ui.adapter.SelectDeptUserAdapter
import com.lxj.xpopup.XPopup
import kotlinx.android.synthetic.main.activity_select_dept_user.*
import me.yokeyword.indexablerv.IndexableLayout
import org.jetbrains.anko.toast

/**
 * 案件管理-派遣人选择界面
 * @author YangWei
 * @date 2019/4/20
 */
class SelectDeptUserActivity : BaseMvpActivity<SelectDeptUserPresenter>(),
    SelectDeptUserView {
    companion object {
        const val TAG = "SelectDeptUserActivity"
    }

    private lateinit var mAdapter: SelectDeptUserAdapter
    private var userName: String? = null
    private var userId: Long = 0L
    private var caseId: Long = 0L
    private var disposeDeptId: Long = 0L // 处置人所在单位
    private var disposeUserId: Long = 0L // 处置人id

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_select_dept_user)

        initView()

        initData()
    }

    /**
     * 初始化视图
     */
    private fun initView() {

        // 导航右侧按钮点击事件注册
        selectDeptUserHeaderBar.getRightView().onClick {
            userName?.let {
                XPopup.Builder(this)
                    .asConfirm(
                        "是否派遣[$userName]处置案件"
                        , ""
                    ) {
                        mPresenter.saveCaseDisposeUser(caseId, userId, disposeDeptId, disposeUserId)
                    }.show()
            }

        }

        // 初始化联系人索引选择器
        val indexLayout = selectDeptUserIndexLayout as IndexableLayout
        indexLayout.setLayoutManager(LinearLayoutManager(this))

        // setAdapter
        mAdapter = SelectDeptUserAdapter(this)
        indexLayout.setAdapter(mAdapter)

        // set Material Design OverlayView
        indexLayout.setOverlayStyle_MaterialDesign(Color.BLUE)

        // 全字母排序。  排序规则设置为：每个字母都会进行比较排序；速度较慢
        indexLayout.setCompareMode(IndexableLayout.MODE_FAST)

        // set Listener
        // View v, int originalPosition, int currentPosition, T entity)
        mAdapter.setOnItemContentClickListener { v, oPos, cPos, entity ->
            // 查看联系人详情
            this.userName = entity.name
            // 设置派遣人员信息
            this.disposeUserId = entity.id
            this.disposeDeptId = entity.deptId

            // 取消其它人员列表选中状态
            mAdapter.items.forEach {
                it.isChecked = false
            }
            entity.isChecked = !entity.isChecked
            mAdapter.notifyDataSetChanged()
        }

    }

    /**
     * 初始化数据
     */
    private fun initData() {
        // 获取案件id
        this.caseId = intent.getLongExtra("caseId", 0L)
        // 当前操作人id
        this.userId = AppPrefsUtils.getLong(BaseConstant.KEY_SP_USER_TOKEN)
        // 获取当前操作人所在单位可派遣人员列表
        mPresenter.selectCaseDispatchUserList(caseId, userId)
    }


    /**
     * Dagger2 注入
     */
    override fun injectComponent() {

        DaggerCaseCenterComponent.builder()
            .activityComponent(mActivityComponent)
            .caseCenterModule(CaseCenterModule())
            .build()
            .inject(this)

        mPresenter.mView = this
    }


    /**
     * 获取数据结果
     */
    override fun onGetSelectCaseDispatchUserListSuccess(result: MutableList<CaseIndexUserInfoVo>) {
        mAdapter.setDatas(result)
    }

    /**
     * 保存成功返回案件详情页面
     */
    override fun onSaveCaseDisposeUserSuccess(result: Boolean) {

        when (result) {
            true -> {
                toast("案件处置人派遣完成")
//                EventBus.getDefault().post(SaveCaseDisposeUserEvent(1))
                finish()
            }
            else -> {
                toast("案件处置人派遣错误,请再重试")
            }
        }
    }
}
